package com.tmt.im.manager.controller.api;

import com.tmt.im.manager.controller.vo.*;
import com.tmt.im.manager.helper.PrincipalHelper;
import com.tmt.im.manager.service.IMUserService;
import com.tmt.springboot.common.restful.IExecuteResult;
import com.tmt.springboot.common.restful.ResultWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.scheduling.annotation.Async;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.concurrent.CompletableFuture;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/8/4 11:18
 * @Version 1.0
 **/
@Async("asyncTaskExecutor")
@Tag(name = "用户管理", description = "用户相关接口")
@RestController
@RequestMapping("${api-url-prefix}/user")
public class UserController {

    private final IMUserService imUserService;

    public UserController(IMUserService imUserService) {
        this.imUserService = imUserService;
    }

    //@PreAuthorize("hasRole('ROLE_TMT_ROLE_ADMIN')")
    @Operation(summary = "初始化默认用户列表", description = "初始化默认用户列表")
    @GetMapping(value = "/load")
    @Deprecated
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> loadUserList(Principal principal) {
        IExecuteResult<Boolean> result = imUserService.loadUserList(principal);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "获取当前登录用户信息",
            description = "获取当前登录用户信息",
            parameters = {
                    @Parameter(name = "deviceType", description = "用户当前的设备类型", required = true),
                    @Parameter(name = "deviceId", description = "用户当前的设备ID", required = true),
            })
    @GetMapping(value = "/userInfoEx/{deviceType}/{deviceId}")
    public CompletableFuture<IExecuteResult<UserInfoVoEx>> getUserInfoEx(@PathVariable("deviceType") String deviceType,
                                                                         @PathVariable("deviceId") String deviceId,
                                                                         Principal principal) {
        Long userId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<UserInfoVoEx> result = imUserService.getUserInfoEx(userId, deviceType, deviceId);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "异地登录验证接口",
            description = "异地登录验证接口",
            parameters = {
                    @Parameter(name = "OffsiteCheckRequest", description = "异地登录验证请求对象", required = true)
            })
    @PostMapping(value = "/offsiteCheck")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> checkOffsiteLogin(@RequestBody @Validated OffsiteCheckRequest request,
                                                                                       Principal principal) {
        Long userId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<Boolean> result = imUserService.checkOffsiteLogin(userId, request);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "查询条件指定的用户信息",
            description = "按条件精确查询用户信息",
            operationId = "queryUserByCondition",
            parameters = {
                    @Parameter(name = "request", description = "查询条件对象", required = true)
            })
    @PostMapping(value = "/search")
    public CompletableFuture<IExecuteResult<SlideConditionQueryUserResponse>> queryUserByCondition(@RequestBody @Validated SlideConditionQueryRequest1 request,
                                                                                                   Principal principal) {
        IExecuteResult<SlideConditionQueryUserResponse> result = imUserService.slideQueryUserByCondition(request, principal);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "按手机号码或会员编号查询用户信息",
            description = "按完整的手机号码或完整的会员编号查询用户信息",
            operationId = "searchUserByCondition",
            parameters = {
                    @Parameter(name = "mobileOrCode", description = "完整的手机号码或完整的会员编号", required = true)
            })
    @GetMapping(value = "/search/{mobileOrCode}")
    public CompletableFuture<IExecuteResult<UserInfoVoEx1>> searchUserByCondition(@PathVariable("mobileOrCode") String mobileOrCode,
                                                                                  Principal principal) {
        IExecuteResult<UserInfoVoEx1> result = imUserService.searchUserByCondition(mobileOrCode, principal);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "按用户ID查询用户信息",
            description = "按用户ID查询用户信息",
            operationId = "queryUserById",
            parameters = {
                    @Parameter(name = "userId", description = "目标用户ID", required = true)
            })
    @GetMapping(value = "/query/{userId}")
    public CompletableFuture<IExecuteResult<UserInfoVoEx1>> queryUserById(@PathVariable("userId") Long userId, Principal principal) {
        IExecuteResult<UserInfoVoEx1> result = imUserService.queryUserById(userId, principal);
        return CompletableFuture.completedFuture(result);
    }

}
